﻿using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace Analyzer.ViewModel
{
    public class AssemblyViewModel : ViewModelBase
    {
        #region Nested Types

        private class ItemsControlMouseLeftButtonDownCommand : ICommand
        {
            private AssemblyViewModel mParent;

            public ItemsControlMouseLeftButtonDownCommand(AssemblyViewModel parent)
            {
                mParent = parent;
            }

            public void Execute(object parameter)
            {
                mParent.IsSelected = !mParent.IsSelected;
                MessageBox.Show("IsSelected == " + mParent.IsSelected.ToString());
            }

            public bool CanExecute(object parameter)
            {
                return true;
            }

            public event EventHandler CanExecuteChanged;
        }

        #endregion Nested Types

        #region Lifecycle

        public AssemblyViewModel() { }

        #endregion Lifecycle

        #region Properties

        private string pFilePath;
        /// <summary>
        /// Sets or gets the string describing the path of the file ending with the file name.
        /// </summary>
        public string FilePath
        {
            get { return pFilePath; }
            set
            {
                if (value.Equals(pFilePath) == false)
                {
                    pFilePath = value;
                    string extension = Path.GetExtension(pFilePath);
                    string pathWithoutExtension = pFilePath.Substring(0, pFilePath.Length - extension.Length);
                    this.ProgramDatabaseFilePath = pathWithoutExtension + ".pdb";
                    if (File.Exists(this.ProgramDatabaseFilePath) == false)
                        this.ProgramDatabaseFilePath = null;

                    OnPropertyChanged("FilePath");
                    OnPropertyChanged("FileName");
                    OnPropertyChanged("Warning");
                    OnPropertyChanged("HasWarning");
                    OnPropertyChanged("HasProgramDatabase");
                    OnPropertyChanged("ProgramDatabaseFilePath");
                    OnPropertyChanged("ProgramDatabaseFileName");
                }
            }
        }

        /// <summary>
        /// Gets the name of the file.
        /// </summary>
        public string FileName
        {
            get
            {
                return this.FilePath.Split('\\').Last();
            }
        }


        /// <summary>
        /// Gets wether the file has a corresponding .pdb file or not.
        /// </summary>
        public bool HasProgramDatabase
        {
            get { return this.ProgramDatabaseFilePath != null; }
        }

        /// <summary>
        /// Gets the path of the .pdb file, if it exist, else null.
        /// </summary>
        public string ProgramDatabaseFilePath { get; private set; }

        /// <summary>
        /// Gets the name of the .pdb file, if it exists, else null.
        /// </summary>
        public string ProgramDatabaseFileName
        {
            get
            {
                if (this.HasProgramDatabase == true)
                    return this.ProgramDatabaseFilePath.Split('\\').Last();
                else
                    return null;
            }
        }

        /// <summary>
        /// Indicates wether the file needs to be deleted after the analysis, or not.
        /// </summary>
        public bool DeleteIsNeeded { get; set; }

        /// <summary>
        /// Indicates wether the .pdb file needs to be deleted after the analysis, or not.
        /// </summary>
        public bool ProgramDatabaseDeleteIsNeeded { get; set; }


        /// <summary>
        /// Gets a warning message indicating that the file is an assembly.
        /// </summary>
        /// <remarks>
        /// Empty string indicates no problem.
        /// Any other case indicates some problem.
        /// </remarks>
        public string Warning
        {
            get
            {
                string fileExtension = this.FileName.Split('.').Last();
                if ((fileExtension.Equals("exe") || fileExtension.Equals("dll")) == false)
                    return "A fájl kiterjesztése nem exe vagy dll !";
                else
                    return "";
            }
        }

        /// <summary>
        /// Gets a value indicating that there is problem with the file or not.
        /// </summary>
        public bool HasWarning
        {
            get
            {
                return this.Warning.Equals("") == false;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating that the file is selected.
        /// </summary>
        private bool pIsSelected;
        public bool IsSelected
        {
            get { return pIsSelected; }
            set
            {
                if (value != pIsSelected)
                {
                    pIsSelected = value;
                    OnPropertyChanged("IsSelected");
                }
            }
        }

        #endregion Properties

        #region Commands

        private ICommand pSelectCommand;
        public ICommand SelectCommand
        {
            get
            {
                if (pSelectCommand == null)
                    pSelectCommand = new ItemsControlMouseLeftButtonDownCommand(this);
                return pSelectCommand;
            }
        }

        #endregion Commands
    }
}
